package com.sgb.member.view_model;


import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.MemberBrandLevel;
import com.sgb.member.entity.MemberBusinessBean;
import com.sgb.member.entity.MemberBusinessJingYingBean;
import com.sgb.member.entity.MemberMaterEntity;
import com.sgb.member.ui.fragment.BusinessGoodsFragment2;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;


public class BusinessGoodsFragModel2 extends BaseViewModel {

    private final BusinessGoodsFragment2 fragment;
    private final MemberRepertory mRepertory;
    private final int maxPageSize = 10;
    private int current;
    private CommonPopEntity mAll;

    public BusinessGoodsFragModel2(BusinessGoodsFragment2 fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    // 经营商品行业分类
    public void getOneCategory(int type) {
        showOrHideDialog(true);
        mRepertory.getApi().getIndustryList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    //  根据行业分类->商品类型（industryId:一级类目id，areaCode:省份id)
    //类型
    public void getFirstCategory(int type, String id, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("industryId", id);
        map.put("areaCode", mRepertory.getAreaCode());
        mRepertory.getApi().getFirstCategory(map).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    // 类别
    public void getCategoryChoose(int type, String id, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getCategoryChoose(id, mRepertory.getAreaCode()).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                } else {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, new ArrayList<>());
                    } else {
                        fragment.setChooseItemData(type, new ArrayList<>());
                    }
                }
            }
        });
    }

    // 品牌分级
    public void getBrandData(int type, boolean isShowLeftDialog) {
        mRepertory.getApi().getGoodsBrandLevel().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (data.getData().size() > 0) {
                        CommonPopEntity b = data.getData().get(0);
                        if (b.getTypeName().equals("不限")) {
                            data.getData().get(0).setTypeName("全部");
                        }
                    }
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                } else {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, new ArrayList<>());
                    } else {
                        fragment.setChooseItemData(type, new ArrayList<>());
                    }
                }
            }
        });
    }

    // 品牌名称
    public void getBrandNameData(int type, String id,
                                 String firstCategory,
                                 String secondCategory,
                                 String thirdCategory, boolean isShowLeftDialog) {
        Map<String, Object> map = new HashMap<>();
        map.put("firstCategory", firstCategory);
        map.put("secondCategory", secondCategory);
        map.put("thirdCategory", thirdCategory);
        map.put("type", id);
        mRepertory.getApi().getMemberBrandLevelByGoodsCategory(map).enqueue(new ICallBack<BaseEntity<List<MemberBrandLevel>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MemberBrandLevel>> data) {
                if (data.getData() != null && data.getData() != null) {
                    List<MemberBrandLevel> list = data.getData();
                    List<CommonPopEntity> datas = new ArrayList<>();
                    if (list.size() > 0) {
                        datas.add(new CommonPopEntity("", "全部"));
                        for (MemberBrandLevel b : list) {
                            datas.add(new CommonPopEntity(b.getBrandNo(), b.getBrandName()));
                        }
                    }
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, datas);
                    } else {
                        fragment.setChooseItemData(type, datas);
                    }
                } else {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, new ArrayList<>());
                    } else {
                        fragment.setChooseItemData(type, new ArrayList<>());
                    }
                }
            }
        });
    }

    //会员级别
    public void getMemberLevelData(int type, boolean isShowLeftDialog) {
        mRepertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                } else {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, new ArrayList<>());
                    } else {
                        fragment.setChooseItemData(type, new ArrayList<>());
                    }
                }
            }
        });
    }

    // 品名->计量方式/规格/材质/...
    public void getCateArguments(int type, String id, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getMaterChoose(id, mRepertory.getAreaCode()).enqueue(new ICallBack<BaseEntity<List<MemberMaterEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MemberMaterEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> list = new ArrayList<>();
                    for (MemberMaterEntity datum : data.getData()) {
                        List<CommonPopEntity> childList = new ArrayList<>();
                        List<MemberMaterEntity.TypeBean> typeList = datum.getType();
                        if (typeList != null && typeList.size() > 0) {
                            for (MemberMaterEntity.TypeBean typeBean : typeList) {
                                childList.add(new CommonPopEntity(typeBean.getCid(), typeBean.getCname()));
                            }
                        }
                        CommonPopEntity popEntity = new CommonPopEntity(datum.getId(), datum.getName());
                        popEntity.setChildren(childList);
                        list.add(popEntity);
                    }
                    fragment.setMultiChooseItemData(type, list, mAll);
                }
                //   getBrand(type, id, isShowLeftDialog);
            }
        });
    }

    // 价格排序
    public void getPriceData(int type) {
//        showOrHideDialog(true);
//        mRepertory.getApi().getMaterialPriceData().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
//            @Override
//            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
//                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
//                    fragment.setChooseItemData(type, data.getData());
//                }
//            }
//        });
        List<CommonPopEntity> data =new ArrayList<>();
        data.add(new CommonPopEntity("","最新发布"));
        data.add(new CommonPopEntity("1","价格最低"));
        data.add(new CommonPopEntity("2","价格最高"));
        fragment.setChooseItemData(type, data);
    }

    // 距离
    public void getDistanceData(int type) {
        showOrHideDialog(true);
        mRepertory.getApi().getMaterialDistanceData().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData());
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
//    public void getAddressModel(int type) {
//        showOrHideDialog(true);
//        mRepertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
//            @Override
//            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
//                showOrHideDialog(false);
//                if (data != null && data.getData() != null && data.getData().size() > 0) {
//                    List<CommonPopEntity> entities = new ArrayList<>();
//                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
//                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
//                    }
//                    entities.add(0, mAll);
//                    fragment.setChooseItemData(type, entities);
//                    fragment.setAddressData(data.getData());
//                }
//            }
//        });
//    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, entities);
                    } else {
                        fragment.setChooseItemData(type, entities);
                    }
                }
            }
        }
    }

    public void getAddressAreaData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, String cityNo, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> childrenCity = scrDatum.getChildren();
                if (childrenCity != null && childrenCity.size() > 0) {
                    for (AddressEntity<List<AddressEntity>> child : childrenCity) {
                        if (child.getId().equals(cityNo)) {
                            List<AddressEntity> childArea = child.getChildren();
                            if (childArea != null && childArea.size() > 0) {
                                List<CommonPopEntity> entities = new ArrayList<>();
                                for (AddressEntity area : childArea) {
                                    entities.add(new CommonPopEntity(area.getNo(), area.getSimpleName()));
                                }
                                entities.add(0, mAll);
                                if (isShowLeftDialog) {
                                    fragment.showLeftDialog(type, entities);
                                } else {
                                    fragment.setChooseItemData(type, entities);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public void getGoodList(int flag, boolean isLoadMore, String companyName, String mNo0,
                            String mNo1, String mNo2, String mNo3, List<String> mNo4, String mNo5,
                            String mNo51, String mNo6, String mNo7, String mPromotions, String mBestSellers,
                            boolean isFirst, String lat, String lon,String brand_type) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);

        Map<String, Object> map = new HashMap<>();
        map.put("distanceSortType", mNo6); //距离排序
        if(TextUtils.isEmpty(mNo7)){
            map.put("sortType", "1");//排序方式1按照时间 2按照价格
            map.put("priceSortType", "");
        }else {
            map.put("sortType", "2");
            map.put("priceSortType", mNo7);//价格排序方式
        }
      //  map.put("areacode", mRepertory.getAreaCode());
        map.put("asc", false);
        map.put("type", brand_type);
        map.put("brandIds", mNo4); // 商品分类品名分类子集合
     //   map.put("industryId", mNo0);//行业Id
        map.put("item_cid1", mNo1);//一级类目id
        map.put("item_cid2", mNo2);
        map.put("item_cid", mNo3);//三级类目id
        if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lon)) {
            map.put("lat", lat);
            map.put("lon", lon);
        }
        map.put("key", "");// key就是模糊匹配的
        map.put("page", currentPage);
        map.put("limit", maxPageSize);
        map.put("supplierName", "");
        map.put("title", companyName);//商品搜索关键词
        mRepertory.getApi().getOwnCommodityList(map).enqueue(new ICallBack<BaseEntity<MemberBusinessJingYingBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberBusinessJingYingBean> data) {
                current = data.getData().getPageNumber();
                int total = data.getData().getTotalPage();
               List<MemberBusinessBean> list = data.getData().getList();
                fragment.refreshRecycler(list, isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberBusinessJingYingBean>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }
}
